﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;

namespace FTBS.Models.DAO
{
    public class PriceTicketConcertEntityDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();
        private PriceTicketDAO _priceTicketDao = new PriceTicketDAO();

        public List<PriceTicketConcertEntity> ShowAllPriceTickets()
        {
            List<PriceTicketConcertEntity> listResult = new List<PriceTicketConcertEntity>();
            
            var theaters = from t in _entities.Theaters where t.IsDeleted == false select t;
            List<Theater> allTheaters = new List<Theater>();
            if (theaters != null)
            {
                allTheaters = theaters.ToList();
            }

            var chairTypes = from ct in _entities.ChairTypes where ct.IsDeleted == false select ct;
            List<ChairType> allChairTypes = new List<ChairType>();
            if (chairTypes != null)
            {
                allChairTypes = chairTypes.ToList();
            }

            for (int i = 0; i < allTheaters.Count(); i++)
            {
                PriceTicketConcertEntity priceTicketEntity = new PriceTicketConcertEntity();
                priceTicketEntity.TheaterId = allTheaters[i].TheaterId;
                priceTicketEntity.TheaterName = allTheaters[i].TheaterName;
                if (_entities.PriceTickets.Count(pt => pt.TheaterId == priceTicketEntity.TheaterId) > 0)
                {
                    for (int j = 0; j < allChairTypes.Count();)
                    {
                        priceTicketEntity.ChairTypeId1 = allChairTypes[j].ChairTypeId;
                        priceTicketEntity.ChairTypeName1 = allChairTypes[j].TypeName;
                        priceTicketEntity.Price1 = _priceTicketDao.GetPriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId1);
                        priceTicketEntity.SalePrice1 = _priceTicketDao.GetSalePriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId1);
                        j++;
                        priceTicketEntity.ChairTypeId2 = allChairTypes[j].ChairTypeId;
                        priceTicketEntity.ChairTypeName2 = allChairTypes[j].TypeName;
                        priceTicketEntity.Price2 = _priceTicketDao.GetPriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId2);
                        priceTicketEntity.SalePrice2 = _priceTicketDao.GetSalePriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId2);
                        j++;
                        priceTicketEntity.ChairTypeId3 = allChairTypes[j].ChairTypeId;
                        priceTicketEntity.ChairTypeName3 = allChairTypes[j].TypeName;
                        priceTicketEntity.Price3 = _priceTicketDao.GetPriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId3);
                        priceTicketEntity.SalePrice3 = _priceTicketDao.GetSalePriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId3);
                        j++;

                    }
                    listResult.Add(priceTicketEntity);
                }
            }
            return listResult.ToList();
        }

        public List<PriceTicketConcertEntity> LoadPriceTicketsByTheaterId(int theaterId)
        {
            List<PriceTicketConcertEntity> listResult = new List<PriceTicketConcertEntity>();

            var theaters = from t in _entities.Theaters where t.IsDeleted == false && t.TheaterId == theaterId select t;
            List<Theater> allTheaters = new List<Theater>();
            if (theaters != null)
            {
                allTheaters = theaters.ToList();
            }

            var chairTypes = from ct in _entities.ChairTypes where ct.IsDeleted == false select ct;
            List<ChairType> allChairTypes = new List<ChairType>();
            if (chairTypes != null)
            {
                allChairTypes = chairTypes.ToList();
            }

            for (int i = 0; i < allTheaters.Count(); i++)
            {
                PriceTicketConcertEntity priceTicketEntity = new PriceTicketConcertEntity();
                priceTicketEntity.TheaterId = allTheaters[i].TheaterId;
                priceTicketEntity.TheaterName = allTheaters[i].TheaterName;
                if (_entities.PriceTickets.Count(pt => pt.TheaterId == priceTicketEntity.TheaterId) > 0)
                {
                    for (int j = 0; j < allChairTypes.Count(); )
                    {
                        priceTicketEntity.ChairTypeId1 = allChairTypes[j].ChairTypeId;
                        priceTicketEntity.ChairTypeName1 = allChairTypes[j].TypeName;
                        priceTicketEntity.Price1 = _priceTicketDao.GetPriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId1);
                        priceTicketEntity.SalePrice1 = _priceTicketDao.GetSalePriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId1);
                        j++;
                        priceTicketEntity.ChairTypeId2 = allChairTypes[j].ChairTypeId;
                        priceTicketEntity.ChairTypeName2 = allChairTypes[j].TypeName;
                        priceTicketEntity.Price2 = _priceTicketDao.GetPriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId2);
                        priceTicketEntity.SalePrice2 = _priceTicketDao.GetSalePriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId2);
                        j++;
                        priceTicketEntity.ChairTypeId3 = allChairTypes[j].ChairTypeId;
                        priceTicketEntity.ChairTypeName3 = allChairTypes[j].TypeName;
                        priceTicketEntity.Price3 = _priceTicketDao.GetPriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId3);
                        priceTicketEntity.SalePrice3 = _priceTicketDao.GetSalePriceTicket(priceTicketEntity.TheaterId,
                                                                                  priceTicketEntity.ChairTypeId3);
                        j++;

                    }
                    listResult.Add(priceTicketEntity);
                }
            }
            return listResult.ToList();
        }
    }
}